//
//  ModelExtension.swift
//  AnShiPai
//
//  Created by 马瑜 on 2017/7/6.
//  Copyright © 2017年 马瑜. All rights reserved.
//

import UIKit
import Foundation
import RxSwift
import Moya
import HandyJSON

extension Response {
    
    func mapModel<T: HandyJSON>(_ type: T.Type) -> T {
        let jsonString = String(data: data, encoding: .utf8)
        return JSONDeserializer<T>.deserializeFrom(json: jsonString)!
    }
    
}

extension ObservableType where E == Response {
    public func mapModel<T: HandyJSON>(_ type: T.Type) -> Observable<T> {
        return flatMap({ (response) -> Observable<T> in
            return Observable.just(response.mapModel(T.self))
        })
        
    }
}
